open Base

type pos = { x : int ; y : int }
and dim = { w : int ; h : int }
and aabb = { bx : int ; by : int ; bw : int ; bh : int }

module Pos = struct
  type t = pos
  let zero = { x = 0 ; y = 0 }
  let to_string {x;y} =
    Printf.sprintf "<%d,%d>" x y
  let equal a b = Int.(a.x = b.x && a.y = b.y)
end

module Dim = struct
  type t = dim
  let zero = { w = 0 ; h = 0 }
  let to_string {w;h} =
    Printf.sprintf "<%dx%d>" w h
  let equal a b = Int.(a.w = b.w && a.h = b.h)
end

module AABB = struct
  type t = aabb
  let zero = { bx = 0 ; by = 0 ; bw = 0 ; bh = 0 }

  let make {x;y} {w;h} =
    { bx = x ; by = y
    ; bw = w ; bh = h }

  let to_string {bx;by;bw;bh} =
    Printf.sprintf "<%d,%d %dx%d>" bx by bw bh

  let equal a b = Int.(a.bx = b.bx && a.by = b.by
                       && a.bw = b.bw && a.bh = b.bh)

  let contains {bx;by;bw;bh} {x;y} =
    x >= bx && x <= bx + bw && y >= by && y <= by + bh

  let center {bx;by;bw;bh} {w;h} =
    { bx = max bx (bx + (bw - w) / 2)
    ; by = max by (by + (bh - h) / 2)
    ; bw = min bw w
    ; bh = min bh h }
end
